<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取广告位表模型
$table_position = C::t('#erling_gridad_combo#position');

// 处理表单提交
if(submitcheck('positionsubmit')) {
    $id = $_GET['id'];
    $data = array(
        'position' => $_GET['position'],
        'name' => $_GET['name'],
        'type' => $_GET['type'],
        'sort' => intval($_GET['sort']),
        'status' => intval($_GET['status']),
        'allowed_groups' => is_array($_GET['allowed_groups']) ? implode(',', $_GET['allowed_groups']) : '',
        'daily_credit' => intval($_GET['daily_credit']),
        'monthly_credit' => intval($_GET['monthly_credit']),
        'grid_columns' => intval($_GET['grid_columns']),
        'grid_rows' => intval($_GET['grid_rows']),
        'empty_text' => $_GET['empty_text'],
        'default_color' => $_GET['default_color'],
        'default_bgcolor' => $_GET['default_bgcolor'],
        'random_sort' => intval($_GET['random_sort'])
    );
    
    if($id) {
        $table_position->update($id, $data);
    } else {
        $id = $table_position->insert($data, true);
    }
    
    cpmsg('广告位保存成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_combo&pmod=admin_position', 'succeed');
}

// 添加广告位的接口
if($_GET['pmod'] == 'save_position' && submitcheck('positionsubmit')) {
    $id = intval($_POST['id']);
    $data = array(
        'position' => intval($_POST['position']),
        'name' => dhtmlspecialchars(trim($_POST['name'])),
        // 其他字段...
    );
    
    if($id > 0) {
        C::t('#erling_gridad_combo#position')->update($id, $data);
    } else {
        C::t('#erling_gridad_combo#position')->insert($data);
    }
    exit;
}

// 处理删除操作
if($_GET['type'] == 'delete' && $_GET['id']) {
    $table_position->delete($_GET['id']);
    cpmsg('广告位删除成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_combo&pmod=admin_position', 'succeed');
}

// 获取广告位列表
$positionlist = $table_position->fetch(array(), 'sort ASC, id ASC');

// 获取单个广告位
if($_GET['pmod'] == 'get_position' && $_GET['id']) {
    $position = C::t('#erling_gridad_combo#position')->fetch_one(array('id' => intval($_GET['id'])));
    echo json_encode($position);
    exit;
}

// 准备模板数据
$position_map = array(
    2 => '首页头部',
    3 => '首页中部', 
    4 => '首页底部',
    5 => '帖子页右侧'
);

$type_map = array(
    'text' => '文字格子广告',
    'image' => '图片格子广告',
    'banner' => '横幅滚动广告'
);

// 处理数据用于模板
foreach($positionlist as &$position) {
    $position['position_name'] = $position_map[$position['position']] ?? '未知位置';
    $position['type_name'] = $type_map[$position['type']] ?? $position['type'];
    $position['status_text'] = $position['status'] == 1 ? '启用' : '停用';
}

// 获取用户组数据时添加类型标识
$query = DB::query("SELECT * FROM ".DB::table('common_usergroup'));
while($group = DB::fetch($query)) {
    $_var['cache']['usergroups'][$group['groupid']] = array_merge($group, array(
        'type' => in_array($group['groupid'], array(9,10,11,12,13,14,15)) ? 'member' : 'special'
    ));
}

// 显示模板
include template('erling_gridad_combo:admin_position');